package zack.project.domain.activity.service.quota;

import zack.project.domain.activity.adapter.repository.IActivityRepository;
import zack.project.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import zack.project.domain.activity.model.entity.*;
import zack.project.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import zack.project.domain.activity.service.quota.policy.ITradePolicy;
import zack.project.domain.activity.service.quota.rule.factory.DefaultActivityChainFactory;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class RaffleActivityAccountQuotaService extends AbstractRaffleActivityAccountQuota {
    public RaffleActivityAccountQuotaService(IActivityRepository activityRepository, DefaultActivityChainFactory defaultActivityChainFactory, Map<String, ITradePolicy> tradePolicyGroup) {
        super(activityRepository, defaultActivityChainFactory, tradePolicyGroup);
    }

    @Override
    protected CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity,
                                                            ActivitySkuEntity activitySkuEntity,
                                                            ActivityEntity activityEntity,
                                                            ActivityCountEntity activityCountEntity) {
        //构建活动充值单
        ActivityOrderEntity activityOrderEntity = new ActivityOrderEntity();
        activityOrderEntity.setSku(skuRechargeEntity.getSku());
        activityOrderEntity.setUserId(skuRechargeEntity.getUserId());
        //活动参与单的外部单号由sku商品充值单透传进来
        activityOrderEntity.setOutBusinessNo(skuRechargeEntity.getOutBusinessNo());
        activityOrderEntity.setActivityId(activityEntity.getActivityId());
        activityOrderEntity.setActivityName(activityEntity.getActivityName());
        activityOrderEntity.setStrategyId(activityEntity.getStrategyId());
        //根据sku配置的抽奖次数来设置要充值的次数
        activityOrderEntity.setDayCount(activityCountEntity.getDayCount());
        activityOrderEntity.setMonthCount(activityCountEntity.getMonthCount());
        activityOrderEntity.setTotalCount(activityCountEntity.getTotalCount());
        activityOrderEntity.setOrderTime(new Date());
        activityOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
        activityOrderEntity.setPayAmount(activitySkuEntity.getProductAmount());

        return CreateQuotaOrderAggregate.builder()
                .activityId(activityOrderEntity.getActivityId())
                .userId(activityOrderEntity.getUserId())
                .dayCount(activityCountEntity.getDayCount())
                .monthCount(activityCountEntity.getMonthCount())
                .totalCount(activityCountEntity.getTotalCount())
                .activityOrderEntity(activityOrderEntity)
                .build();
    }


    @Override
    public void clearActivitySkuStock(Long sku) {
        activityRepository.clearActivitySkuStock(sku);
    }

    @Override
    public ActivitySkuStockKeyVO takeQueueValue() throws InterruptedException {
       return  activityRepository.takeQueueValue();
    }

    @Override
    public void clearQueueValue() {
        activityRepository.clearQueueValue();
    }

    @Override
    public void updateActivitySkuStock(Long sku) {
        activityRepository.updateActivitySkuStock(sku);
    }

    @Override
    public Integer queryRaffleActivityAccountDayPartakeCount(Long activityId, String userId) {
       return  activityRepository.queryRaffleActivityAccountDayPartakeCount(activityId,userId);
    }

    @Override
    public ActivityAccountEntity queryActivityAccountEntity(String userId, Long activityId) {
        return activityRepository.queryActivityAccountEntity(userId,activityId);
    }

    @Override
    public Integer queryRaffleActivityAccountPartakeCount(Long activityId, String userId) {
       return activityRepository.queryRaffleActivityAccountPartakeCount(activityId,userId);
    }

    @Override
    public void updateOrder(DeliveryOrderEntity deliveryOrderEntity) {
        activityRepository.updateOrder(deliveryOrderEntity);
    }
}
